package com.account.book.util;

import cn.hutool.core.date.DateUtil;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
import java.util.Locale;

/**
 * @author liang-hw
 * @className TimeToolUtil
 * @description 时间工具类
 * @date 2023/10/16 14:09:08
 */
public class TimeToolUtil {
    /**
     * 获取本月的开始日期
     *
     * @return
     */
    public static String getOneDayOfMonth() {
        LocalDate date = LocalDate.now();
        LocalDate with = date.with(TemporalAdjusters.firstDayOfMonth());
        return LocalDateTime.of(with, LocalTime.MIN).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 获取本月的结束日期
     *
     * @return
     */
    public static String getEndDayOfMonth() {
        LocalDate date = LocalDate.now();
        LocalDate with = date.with(TemporalAdjusters.lastDayOfMonth());
        return LocalDateTime.of(with, LocalTime.MAX).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * @param @param year
     * @return @return {@link String }
     * @author liang-hw
     * @description 根据入参年获取开始时间
     * @date 2024/01/11 16:18:17
     */
    public static String getStartOfYear(String year) {
        // 格式化为字符串
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate startOfYear = LocalDate.ofYearDay(Integer.parseInt(year), 1);
        String formattedStartTime = startOfYear.format(formatter);
        return formattedStartTime;
    }

    /**
     * @param @param year
     * @return @return {@link String }
     * @author liang-hw
     * @description 根据入参年获取结束时间
     * @date 2024/01/11 16:19:34
     */
    public static String getEndOfYear(String year) {
        // 格式化为字符串
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate endOfYear = LocalDate.ofYearDay(Integer.parseInt(year) + 1, 1).minusDays(1);
        String formattedEndTime = endOfYear.format(formatter);
        return formattedEndTime;
    }

    /**
     * @param @param date
     * @return @return {@link String }
     * @author liang-hw
     * @description 指定月份开始时间
     * @date 2024/01/11 16:22:22
     */
    public static String getStartOfMonth(String date) {
        // 转换为YearMonth对象
        YearMonth yearMonth = YearMonth.parse(date, DateTimeFormatter.ofPattern("yyyy-MM"));
        // 获取指定月份的第一天
        LocalDate firstDay = yearMonth.atDay(1);
        return firstDay.toString();
    }

    /**
     * @param @param date
     * @return @return {@link String }
     * @author liang-hw
     * @description 指定月份结束时间
     * @date 2024/01/11 16:23:26
     */
    public static String getEndOfMonth(String date) {
        // 转换为YearMonth对象
        YearMonth yearMonth = YearMonth.parse(date, DateTimeFormatter.ofPattern("yyyy-MM"));
        // 获取指定月份的最后一天
        LocalDate lastDay = yearMonth.atEndOfMonth();
        return lastDay.toString();
    }

    /**
     * @param @param dateString
     * @return @return {@link String }
     * @author liang-hw
     * @description 获取日期所在星期几
     * @date 2024/01/11 18:30:38
     */
    public static String getDateToWeek(String dateString) {
        String weekdayString="";
        // 定义要转换的字符串日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            // 调用parse()方法进行转换
            Date date = dateFormat.parse(dateString);
            weekdayString = DateUtil.dayOfWeekEnum(date).toChinese();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return weekdayString;
    }
}
